package com.zjsru.oneDay202203;

import java.util.Arrays;

/**
 * @Author: likew
 * @Date: 2022/3/1
 *
 *
 *  Z 字形变换
 * 输入：s = "PAYPALISHIRING", numRows = 4
 * 输出："PINALSIGYAHRPI"
 * 解释：
 * P     I    N
 * A   L S  I G
 * Y A   H R
 * P     I
 */
public class convert {
    static int N = 1010;
    static char[][] g = new char[N][N];
    static int[] idxs = new int[N];
    public String convert(String s, int numRows) {
        if(numRows == 1) return s;
        int n = s.length();
        Arrays.fill(idxs,0);
        for (int i = 0, j = 0,k = 1; i < n; i++) {
            g[j][idxs[j]++] = s.charAt(i);
            j+=k;
            if(j<0){
                j+=2;k=1;
            }else if(j == numRows){
                j -= 2;k=-1;
            }
        }
        StringBuilder sb = new StringBuilder();
        for(int i = 0;i<numRows;i++){
            for (int j = 0; j < idxs[i]; j++) {
                sb.append(g[i][j]);
            }
        }
        return sb.toString();
    }
    
    public static void main(String[] args) {
        convert convert = new convert();
        String s = "PAYPALISHIRING";
        int numRows = 4;
        System.out.println(convert.convert(s, numRows));
    }

}
